package com.zmn.oms.api.controller.open;

import com.alibaba.fastjson.JSON;
import com.zmn.common.constant.StatusConsts;
import com.zmn.oms.common.constant.OpenApiConsts;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.dto.open.v2.*;
import com.zmn.oms.model.entity.dict.ChannelEntrustInterface;
import com.zmn.oms.zmn.facade.interfaces.open.EntrustOrderNotifyFacade;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Optional;

/**
 * 类描述：委托单接口
 *
 * @author lhl
 * @date 2020/01/10 17:35
 */
@RestController
@RequestMapping("/entrust")
@Slf4j
public class EntrustOrderController extends OrderBaseController {

    private static final String TAG = "oms-open-api-entrust";

    @Resource
    private EntrustOrderNotifyFacade entrustOrderNotifyFacade;

    /**
     * 修改订单状态
     *
     * @param requestDTO 订单信息
     * @return
     */
    @PostMapping("/updateOrderStatus")
    @SuppressWarnings("all")
    public ResultDTO updateOrderStatus(HttpServletRequest request, OpenApiV2Request openApiV2Request) {

        try {
            log.info("[{}-updateOrderStatus] {}", TAG, JSON.toJSONString(openApiV2Request));

            // 验证参数
            ChannelEntrustInterface channelInterface = super.validateEntrustData(request, openApiV2Request, true);

            ResultDTO response = null;
            switch (openApiV2Request.getFunId()) {
                // 修改预约时间
                case OpenApiConsts.FUNID_DUTY_TIME :
                    PushDutyTimeDTO pushDutyTimeDTO = JSON.parseObject(openApiV2Request.getJsonData(),
                            PushDutyTimeDTO.class);
                    response = entrustOrderNotifyFacade.acceptDutyTime(pushDutyTimeDTO);
                    break;
                // 接单工程师信息
                case OpenApiConsts.FUNID_MASTER_INFO :
                    PushMasterInfoDTO pushMasterInfoDTO = JSON.parseObject(openApiV2Request.getJsonData(),
                            PushMasterInfoDTO.class);
                    response = entrustOrderNotifyFacade.acceptMasterInfo(pushMasterInfoDTO);
                    break;
                // 工程师上门
                case OpenApiConsts.FUNID_MASTER_VISIT :
                    PushOrderDTO pushMasterVisitDTO = JSON.parseObject(openApiV2Request.getJsonData(),
                            PushOrderDTO.class);
                    response = entrustOrderNotifyFacade.acceptMasterVisit(pushMasterVisitDTO);
                    break;
                // 订单取消
                case OpenApiConsts.FUNID_ORDER_CANCEL :
                    PushOrderDTO pushOrderCancelDTO = JSON.parseObject(openApiV2Request.getJsonData(),
                            PushOrderDTO.class);
                    response = entrustOrderNotifyFacade.acceptOrderCancel(pushOrderCancelDTO);
                    break;
                // 订单完成
                case OpenApiConsts.FUNID_ORDER_COMPLETE :
                    PushOrderCompleteDTO pushOrderCompleteDTO = JSON.parseObject(openApiV2Request.getJsonData(),
                            PushOrderCompleteDTO.class);
                    response = entrustOrderNotifyFacade.acceptOrderComplete(pushOrderCompleteDTO);
                    break;
                default:
                    log.error("[{}] 参数错误：无法识别的funId。funId=[{}]", TAG, openApiV2Request.getFunId());
                    return ResultDTO.fail(StatusConsts.ERROR_PARAMS, "参数错误：无法识别的funId。");
            }

            log.info("[{}-updateOrderStatus-response] {}", TAG, JSON.toJSONString(response));

            if (response.isStatusBool()) {
                return ResultDTO.success();
            }
            else {
                return ResultDTO.fail("操作失败："+ response.getMessage());
            }
        } catch (OmsBaseException e) {
            log.info("[{}-updateOrderStatus] {}", TAG, e.getMessage());
            return ResultDTO.fail(Optional.ofNullable(e.getCode()).orElse(StatusConsts.STATUS_ERROR), e.getMessage());
        } catch (Exception e) {
            log.error("[{}-updateOrderStatus] {}", TAG, e.getMessage(), e);
            return ResultDTO.fail("操作失败");
        }
    }

}
